package com.yuandian.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateTool {
	/**
	 * 说明：前段展示与数据重组存在一个方法同名同参(yyyy_MM_dd_HH_mm_ssSSS方法)，前端改变了方法名，改为yyyy_MM_dd_HH_mm_ssSSS_Api
	 */
	final static Logger loggerProject = LoggerFactory.getLogger("project");


	/**
	 * 根据开始时间、结束时间、时间间隔 自动生成X轴数据
	 * @param starTime
	 * @param endTime
	 * @param timeInterval 时间间隔
	 * @return
	 */
	public static List<String> getTimeList(String starTime, String endTime, int timeInterval) {

		List<String> timeList = new ArrayList<>();
		int year = 0, month = 0, day = 0, hour = 0, min = 0;
		Date startDate = DateTool.yyyy_MM_dd_HH_mm(starTime);
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.setTime(startDate);
		year = startCalendar.get(Calendar.YEAR);
		month = startCalendar.get(Calendar.MONTH);
		day = startCalendar.get(Calendar.DAY_OF_MONTH);
		hour = startCalendar.get(Calendar.HOUR_OF_DAY);
		// 向前归属
		min = startCalendar.get(Calendar.MINUTE) / timeInterval * timeInterval;
		startCalendar.set(year, month, day, hour, min);
		startDate = startCalendar.getTime();
		starTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startDate);

		Date endDate =DateTool.yyyy_MM_dd_HH_mm(endTime);
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(endDate);
		year = endCalendar.get(Calendar.YEAR);
		month = endCalendar.get(Calendar.MONTH);
		day = endCalendar.get(Calendar.DAY_OF_MONTH);
		hour = endCalendar.get(Calendar.HOUR_OF_DAY);
		// 向前归属
		min = endCalendar.get(Calendar.MINUTE) / timeInterval * timeInterval;
		endCalendar.set(year, month, day, hour, min);
		endDate = endCalendar.getTime();
		endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(endDate);

		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		// 把日期时间格式化
		LocalDateTime start = LocalDateTime.parse(starTime, formatter);
		LocalDateTime end = LocalDateTime.parse(endTime, formatter);

		while(!start.equals(end)) {
			timeList.add(start.toString().replace("T", " ") + ":00.0");
			start = start.plusMinutes(timeInterval);
		}
		timeList.add(end.toString().replace("T", " ") + ":00.0");

		return timeList;
	}

	/**
	 * 根据开始时间、结束时间、时间间隔 自动生成X轴数据
	 * @param starTime
	 * @param endTime
	 * @param timeInterval 时间间隔
	 * @return
	 */
	public static List<String> getTimeListByJDK8(String starTime, String endTime, int timeInterval) {
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		LocalDateTime startDateTime = LocalDateTime.parse(starTime, formatter);
		LocalDateTime endDateTime = LocalDateTime.parse(endTime, formatter);
		List<String> timeList = new ArrayList<>();

		//遍历从startTime 到 endTime 时间间隔的所有时间点
		while (startDateTime.isBefore(endDateTime)){
			timeList.add(startDateTime.format(formatter));
			startDateTime = startDateTime.plusMinutes(timeInterval);
		}
		return timeList;
	}


	/**
	 * 计算时间间隔, 以分钟为单位返回
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static long timeDistance(String startTime, String endTime){
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		// 把日期时间格式化
		LocalDateTime start = LocalDateTime.parse(startTime, formatter);
		LocalDateTime end = LocalDateTime.parse(endTime, formatter);
		Duration minutesDuration = Duration.between(start, end);
		long minutes = minutesDuration.toMinutes();

		return minutes;
	}




	/**
	 * 判断 日期是否是今天
	 * @param dateTimeStr 时间日期
	 * @param formatStr 格式：yyyy-MM-dd   /  yyyyMMdd
	 * @return
	 */
	public static boolean isToday(String dateTimeStr, String formatStr) {
		SimpleDateFormat format = new SimpleDateFormat(formatStr);
		Date date = null;
		try {
			date = format.parse(dateTimeStr);
		}catch (ParseException e) {

		}
		Calendar c1 = Calendar.getInstance();
		c1.setTime(date);
		int year1 = c1.get(Calendar.YEAR);
		int month1 = c1.get(Calendar.MONTH)+1;
		int day1 = c1.get(Calendar.DAY_OF_MONTH);

		Calendar c2 = Calendar.getInstance();
		c2.setTime(new Date());
		int year2 = c2.get(Calendar.YEAR);
		int month2 = c2.get(Calendar.MONTH)+1;
		int day2 = c2.get(Calendar.DAY_OF_MONTH);

		return (year1 == year2 && month1 == month2 && day1 == day2) ? true : false;
	}

	/**
	 * 把日期转换为（yyyy-MM-dd HH:mm:ss）格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String yyyy_MM_dd_HH_mm_ss(Date date) {
		if (date != null) {
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return sdf1.format(date);
		}
		return null;
	}

	public static String yyyy_MM_dd_HH_mm_00(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
		return sdf.format(date);
	}

	public static String yyyyMMddHHmmssSS(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		return sdf.format(date);
	}

	/**
	 * 把日期转换为（yyyy-MM-dd HH:mm）格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String yyyy_MM_dd_HH_mm(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		return sdf.format(date);
	}

	/**
	 * 把日期转换为（yyyy-MM-dd HH）格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String yyyy_MM_dd_HH(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");

		return sdf.format(date);
	}

	/**
	 * 把日期转换为（yyyy-MM-dd HH:00:00）格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String yyyy_MM_dd_HH_00_00(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:00:00");

		return sdf.format(date);
	}

	/**
	 * 把日期转换为（yyyy-MM-dd HH:mm:ssss）格式的字符,精确到毫秒
	 *
	 * @param date
	 * @return
	 */
	public static String yyyy_MM_dd_HH_mm_ssSSS(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		return sdf.format(date);
	}

	/**
	 * 把毫秒转换为（yyyy-MM-dd HH:mm:ssss）格式的字符,精确到毫秒
	 * @return
	 */
	public static String yyyy_MM_dd_HH_mm_ssSSS(Long timeUsec) {
		Date date = new Date(timeUsec);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		return sdf.format(date);
	}

	/**
	 * 把毫秒转换为（yyyy-MM-dd HH:mm:ss）格式的字符,精确到毫秒
	 * @return
	 */
	public static String yyyy_MM_dd_HH_mm_ss(Long timeUsec) {
		Date date = new Date(timeUsec);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
	}

	public static Date yyyy_MM_dd_HH_mm_ssSSS(String dateStr) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
			return sdf.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			loggerProject.info("字符串转化日期出错（yyyy-MM-dd HH:mm:ss.SSS）", e.fillInStackTrace());
			return null;
		}
	}

	/**
	 * 获取指定年月的最后一天
	 * @param year
	 * @param month
	 * @return
	 */
	public static String getLastDayOfMonth(int year,int month) throws Exception {
		String lastDayOfMonth = "";
		try {
			Calendar cal = Calendar.getInstance();
			//设置年份
			cal.set(Calendar.YEAR,year);
			//设置月份
			cal.set(Calendar.MONTH, month-1);
			//获取某月最大天数
			int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
			//设置日历中月份的最大天数
			cal.set(Calendar.DAY_OF_MONTH, lastDay);
			//格式化日期
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			lastDayOfMonth = sdf.format(cal.getTime());

		} catch (Exception e) {
			throw new Exception(e);
		}

		return lastDayOfMonth;
	}

	/**
	 *
	 * 获得下个月的最后天
	 * @return
	 */
	public static Date getMonthLast(){
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH,1);
		c.set(Calendar.DAY_OF_MONTH, 0);
		return c.getTime();
	}
	// =================前端======================

	/**
	 * 把日期转换为（yyyy-MM）格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String yyyy_MM(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		return sdf.format(date);
	}
	//前端重写方法
	public static Date yyyy_MM_dd_HH_mm_ssSSS_Api(String dateStr) {

		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
			if (dateStr.length() == 19) {
				sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			}
			return sdf.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			loggerProject.info("字符串转化日期出错（yyyy-MM-dd HH:mm:ss.SSS）", e.fillInStackTrace());
			return null;
		}
	}

	/**
	 * 把日期转换为（HH:mm:ss）格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String HH_mm(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		return sdf.format(date);
	}
	// =======================================

	/**
	 * 把（yyyy-MM-dd HH:mm:ss）格式的字符串转换为为日期
	 *
	 * @param strDate
	 * @return
	 */
	public synchronized static Date yyyy_MM_dd_HH_mm_ss(String strDate) {
		try {
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return sdf1.parse(strDate);
		} catch (Exception ex) {
			loggerProject.error("字符转换为日期出错(yyyy-MM-dd HH:mm:ss)", ex.fillInStackTrace());
			return null;
		}
	}

	/**
	 * 把（yyyy-MM-dd HH:mm:ss.SSS）格式的字符串转换为（yyyy-MM-dd HH:mm:ss）字符串
	 *
	 * @param strDate
	 * @return
	 */
	public static String yyyy_MM_dd_HH_mm_ssBySSS(String strDate) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = sdf.parse(strDate);
			return yyyy_MM_dd_HH_mm_ss(date);
		} catch (Exception ex) {
			loggerProject.error("字符转换为日期出错(yyyy-MM-dd HH:mm:ss)", ex.fillInStackTrace());
			return null;
		}
	}

	/**
	 * 把（yyyy-MM-dd HH:mm）格式的字符串转换为为日期
	 *
	 * @param strDate
	 * @return
	 */
	public static Date yyyy_MM_dd_HH_mm(String strDate) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			return sdf.parse(strDate);
		} catch (Exception ex) {
			loggerProject.error("字符转换为日期出错(yyyy-MM-dd HH:mm:ss)", ex.fillInStackTrace());
			return null;
		}
	}

	/**
	 * 把（yyyy-MM-dd HH）格式的字符串转换为为日期
	 *
	 * @param strDate
	 * @return
	 */
	public static Date yyyy_MM_dd_HH(String strDate) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
			return sdf.parse(strDate);
		} catch (Exception ex) {
			loggerProject.error("字符转换为日期出错(yyyy-MM-dd HH)", ex.fillInStackTrace());
			return null;
		}
	}

	/**
	 * 把日期转换为（yyyy-MM-dd）格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String yyyy_MM_dd(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(date);
	}

	/**
	 * 把日期转换为（yyyy-MM-dd 00:00:00"）格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String yyyy_MM_dd_00_00_00(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		return sdf.format(date);
	}

	/**
	 * 把日期转换为（yyyy-MM-dd 00:00:00"）格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String yyyy_MM_dd_00_00_00(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		return sdf.format(yyyy_MM_dd_HH_mm_ss(date));
	}


	public static void main(String[] args) {
		System.out.println(yyyy_MM_dd_00_00_00("2022-01-03 11:11:00"));
	}

	/**
	 * 把（yyyy-MM-dd）格式的字符串转换为为日期
	 *
	 * @param strDate
	 * @return
	 */
	public static Date yyyy_MM_dd(String strDate) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			return sdf.parse(strDate);
		} catch (Exception ex) {
			loggerProject.error("字符转换为日期出错(yyyy-MM-dd)", ex.fillInStackTrace());
			return null;
		}
	}


	/**
	 * 把"2021-11-21 12:00:00" 转换成 "2021年11月21日"
	 * @param strDate
	 * @return
	 */
	public static String mm_dd(String strDate) {
		try {
			String dateStr = strDate.substring(0, 10);
			Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
			String resData = new SimpleDateFormat("MM-dd").format(dateVal);
			return resData;
		} catch (Exception ex) {
			loggerProject.error("字符转换为日期出错(MM-dd)", ex.fillInStackTrace());
			return null;
		}
	}

	/**
	 * 把"2021-11-21 12:00:00" 转换成 "2021年11月21日"
	 * @param strDate
	 * @return
	 */
	public static String yyyy_mm_dd(String strDate) {
		try {
			String dateStr = strDate.substring(0, 10);
			Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
			String resData = new SimpleDateFormat("yyyy年MM月dd日").format(dateVal);
			return resData;
		} catch (Exception ex) {
			loggerProject.error("字符转换为日期出错(yyyy年MM月dd日)", ex.fillInStackTrace());
			return null;
		}
	}
	/**
	 * 把（yyyy-MM-dd HH:mm:ss.0）去掉最后的 .0
	 *
	 * @param strDate
	 * @return
	 */
	public static String interceptionTime(String strDate) {
		try {
			String dateStr = strDate.substring(0, 19);
			return dateStr;
		} catch (Exception ex) {
			loggerProject.error("字符日期截取出错(yyyy-MM-dd HH:mm:ss)", ex.fillInStackTrace());
			return null;
		}
	}

	/**
	 * 往后推一天, 时间设为 00:00:00
	 * @param timeStr
	 * @return
	 */
	public static String getAfterDay(String timeStr) {

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		try {
			Date dd = df.parse(timeStr);
			calendar.setTime(dd);
			calendar.add(Calendar.DAY_OF_MONTH, 1);
		}catch (Exception e) {
			e.printStackTrace();
		}
		String st = df.format(calendar.getTime());
		st = st+" 00:00:00";

		return st;
	}

	/**
	 * 往前或者往后推一天, 获取表名_yyyyMMdd
	 * @param timeStr
	 * @return
	 */
	public static String getBeforeAndAfterTableName(String timeStr,Integer dayNum,String tableName) {

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		DateFormat df2 = new SimpleDateFormat("yyyyMMdd");
		Calendar calendar = Calendar.getInstance();
		try {
			Date dd = df.parse(timeStr);
			calendar.setTime(dd);
			calendar.add(Calendar.DAY_OF_MONTH, dayNum);
		}catch (Exception e) {
			e.printStackTrace();
		}
		String st = df2.format(calendar.getTime());
		st = tableName+"_"+st;

		return st;
	}


	public static String yyyyMMdd(Date srcDate) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		return sdf.format(srcDate);
	}

	public static String yyyyMMdd(String srcDate) {
		return srcDate.replaceAll("-", "").substring(0, 8);
	}

	/**
	 * 把日期转换为（HH:mm:ss）格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String HH_mm_ss(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		return sdf.format(date);
	}

	/**
	 * 把日期转换为（mm:ss）格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String mm_ss(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
		return sdf.format(date);
	}

	/**
	 * 把日期转换为（HH）格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String HH(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("HH");
		return sdf.format(date);
	}

	/**
	 * 把（HH:mm:ss）格式的字符串转换为为日期
	 *
	 * @param strDate
	 * @return
	 */
	public static Date HH_mm_ss(String strDate) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
			return sdf.parse(strDate);
		} catch (Exception ex) {
			loggerProject.error("字符转换为日期出错(HH:mm:ss)", ex.fillInStackTrace());
			return null;
		}
	}

	public static String yyyy(Date srcDate) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		return sdf.format(srcDate);
	}

	public static String MM(Date srcDate) {
		SimpleDateFormat sdf = new SimpleDateFormat("MM");
		return sdf.format(srcDate);
	}

	/**
	 * 把日期转换为（yyyyMMddHHmmss）格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String yyyyMMddHHmmss(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		return sdf.format(date);
	}

	/**
	 * 把日期转换为（yyyyMM）格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String yyyyMM(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		return sdf.format(date);
	}

	/**
	 * 把日期转换为（yyyyMMddHHmmss）格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String yyyyMMddHHmmssSSS(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SSS");
		return sdf.format(date);
	}

	/**
	 * 把日期转换为（yyyyMMddHH）格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String yyyyMMddHH(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
		return sdf.format(date);
	}

	/**
	 * 把日期字符串(yyyy-MM-dd HH:mm:ss)转换为（yyyyMMddHHmmss）格式的字符串
	 *
	 * @param strDateTime
	 * @return
	 */
	public static String yyyyMMddHHmmss(String strDateTime) {
		if (strDateTime == null || strDateTime.equals("")) {
			return null;
		}
		String str = strDateTime.replaceAll("-", "");
		str = str.replaceAll(" ", "");
		str = str.replaceAll(":", "");
		return str;
	}

	/**
	 * 把日期转换为(yyyyMMddHHmm)格式的字符串
	 *
	 * @param datetime
	 * @return
	 */
	public static String yyyyMMddHHmm(Date datetime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
		return sdf.format(datetime);
	}

	/**
	 * 把传入时间转化为时间字符串(HHmmss格式)
	 *
	 * @param datetime
	 * @return
	 */
	public static String HHmmss(Date datetime) {
		SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
		return sdf.format(datetime);
	}

	/**
	 * MMdd格式
	 *
	 * @param datetime
	 * @return
	 */
	public static String MMdd(Date datetime) {
		SimpleDateFormat sdf = new SimpleDateFormat("MMdd");
		return sdf.format(datetime);
	}

	/**
	 * 把(yyyyMMdd、yyyyMMddHHmmss)格式转化为(yyyy-MM-dd)字符串
	 *
	 * @param strDatetime
	 * @return
	 */
	public static String toyyyy_MM_dd(String strDatetime) {
		if (strDatetime == null || strDatetime.equals("")) {
			return null;
		}
		StringBuffer str = new StringBuffer(strDatetime.substring(0, 4));
		str.append("-" + strDatetime.substring(4, 6));
		str.append("-" + strDatetime.substring(6, 8));
		return str.toString();
	}

	public static boolean isBetween(String startDate, String endDate, String startDay, String endDay) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			Date start01 = sdf.parse(startDate);
			Date end01 = sdf.parse(endDate);
			Date start02 = sdf.parse(startDay);
			Date end02 = sdf.parse(endDay);
			if (start01.getTime() <= end02.getTime() && end01.getTime() >= start02.getTime()) {
				return true;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return false;
	}

	/**
	 * 时间一是否在时间二 millisecond 前 是true 否 false
	 *
	 * @param millisecond
	 *            毫秒数
	 * @param one
	 *            时间一
	 * @param two
	 *            时间二
	 * @return
	 */
	public static boolean isBefore(int millisecond, Date one, Date two) {
		boolean result = false;
		Calendar cl = Calendar.getInstance();
		cl.setTime(one);
		cl.add(Calendar.MILLISECOND, millisecond);
		result = cl.getTime().before(two);
		return result;
	}



	/***
	 * 根据字符传得到 Timestamp对象
	 *
	 * @param source
	 *            yyyy-MM-dd HH:mi:ss.SSS
	 * @return
	 */
	public static Timestamp getTimestamp(String source) {
		String[] str = source.split("\\.");
		Date date = yyyy_MM_dd_HH_mm_ss(str[0]);
		int nanos = Integer.parseInt(str[1]);
		Timestamp ts = new Timestamp(date.getTime());
		ts.setNanos(nanos);
		return ts;
	}

	/**
	 * 根据时间得到该时间提前多少分钟的时间 例如：如minute是15，即得到的时间是：该时间减去15分钟
	 *
	 * @param nowTime
	 * @param minute
	 * @return
	 */
	@SuppressWarnings("static-access")
	public static Date getBeforeMinute(Date nowTime, int minute) {
		Calendar c = Calendar.getInstance();
		c.setTime(nowTime);
		c.add(c.MINUTE, -minute);
		return c.getTime();
	}

	/**
	 * 根据时间得到该时间提前多少秒的时间 例如：如second是30，即得到的时间是：该时间减去30秒
	 *
	 * @param nowTime
	 * @param second
	 * @return
	 */
	@SuppressWarnings("static-access")
	public static Date getBeforeSecond(Date nowTime, int second) {
		Calendar c = Calendar.getInstance();
		c.setTime(nowTime);
		c.add(c.SECOND, -second);
		return c.getTime();
	}

	/**
	 * 根据时间得到该时间延后多少秒的时间 例如：如second是30，即得到的时间是：该时间加上30秒
	 *
	 * @param nowTime
	 * @param second
	 * @return
	 */
	@SuppressWarnings("static-access")
	public static Date getAfterSecond(Date nowTime, int second) {
		Calendar c = Calendar.getInstance();
		c.setTime(nowTime);
		c.add(c.SECOND, +second);
		return c.getTime();
	}

	/**
	 * 根据时间得到该时间推迟多少分钟的时间 例如：如minute是15，即得到的时间是：该时间减去15分钟
	 *
	 * @param nowTime
	 * @param minute
	 * @return
	 */
	@SuppressWarnings("static-access")
	public static Date getAfterMinute(Date nowTime, int minute) {
		Calendar c = Calendar.getInstance();
		c.setTime(nowTime);
		c.add(c.MINUTE, +minute);
		return c.getTime();
	}

	public static String getAfterMinuteStr(String nowDateTime, int minute){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = sdf.parse(nowDateTime);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MINUTE, minute);
		c.set(Calendar.SECOND, 00);
		return sdf.format(c.getTime());
	}
	public static String getAfterMinute(String nowDateTime, int minute){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
		Date date = null;
		try {
			date = sdf.parse(nowDateTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MINUTE, +minute);
		return sdf.format(c.getTime());
	}

	/**
	 * 根据时间得到该时间提前多少年的时间 例如：如year是1，即得到的时间是：该时间减去1年
	 *
	 * @param nowTime
	 * @param year
	 * @return
	 */
	@SuppressWarnings("static-access")
	public static Date getBeforeYear(Date nowTime, int year) {
		Calendar c = Calendar.getInstance();
		c.setTime(nowTime);
		c.add(c.YEAR, -year);
		return c.getTime();
	}

	/**
	 * 根据时间得到该时间验收多少年的时间 例如：如year是1，即得到的时间是：该时间机上1年
	 *
	 * @param nowTime
	 * @param year
	 * @return
	 */
	@SuppressWarnings("static-access")
	public static Date getAfterYear(Date nowTime, int year) {
		Calendar c = Calendar.getInstance();
		c.setTime(nowTime);
		c.add(c.YEAR, +year);
		return c.getTime();
	}

	/**
	 * 根据时间得到该时间提前多少月的时间 例如：如month是1，即得到的时间是：该时间减去1个月
	 *
	 * @param nowTime
	 * @param month
	 * @return
	 */
	@SuppressWarnings("static-access")
	public static Date getBeforeMonth(Date nowTime, int month) {
		Calendar c = Calendar.getInstance();
		c.setTime(nowTime);
		c.add(c.MONTH, -month);
		return c.getTime();
	}

	/**
	 * 根据时间得到该时间提前多少月的时间
	 * 例如：如day是1，即得到的时间是：该时间减去1个天
	 * @param nowTime
	 * @return
	 */
	public static Date getBeforeDay(Date nowTime, int day){
		Calendar c = Calendar.getInstance();
		c.setTime(nowTime);
		c.add(Calendar.DATE,-day);
		return c.getTime();
	}

	/**
	 *
	 * 获得某个月的最后天 1-当月 2-下个月
	 *
	 * @param
	 * @param month
	 * @return
	 */
	public static Date getMonthLast(int month) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, month);
		c.set(Calendar.DAY_OF_MONTH, 0);
		return c.getTime();
	}

	/**
	 *
	 * 获得上个月的第一天
	 *
	 * @return
	 */
	public static Date getBeforeMonthFirst() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		c.set(Calendar.DAY_OF_MONTH, 1);
		return c.getTime();
	}

	/**
	 *
	 * 获得上个月的最后天
	 *
	 * @return
	 */
	public static Date getBeforeMonthLast() {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.DAY_OF_MONTH, 1);
		c.add(Calendar.DATE, -1);
		return c.getTime();
	}

	/**
	 * 根据时间得到该时间延后多少月的时间 例如：如month是1，即得到的时间是：该时间加上1个月
	 *
	 * @param nowTime
	 * @param month
	 * @return
	 */
	@SuppressWarnings("static-access")
	public static Date getAfterMonth(Date nowTime, int month) {
		Calendar c = Calendar.getInstance();
		c.setTime(nowTime);
		c.add(c.MONTH, +month);
		return c.getTime();
	}

	/**
	 * 根据时间得到该时间提前多少天的时间 例如：如date是1，即得到的时间是：该时间减去1天
	 *
	 * @param nowTime
	 * @param date
	 * @return
	 */
	@SuppressWarnings("static-access")
	public static Date getBeforeDate(Date nowTime, int date) {
		Calendar c = Calendar.getInstance();
		c.setTime(nowTime);
		c.add(c.DATE, -date);
		return c.getTime();
	}

	/**
	 * 根据时间得到该时间延后多少天的时间 例如：如date是1，即得到的时间是：该时间加上1天
	 *
	 * @param nowTime
	 * @param date
	 * @return
	 */
	@SuppressWarnings("static-access")
	public static Date getAfterDate(Date nowTime, int date) {
		Calendar c = Calendar.getInstance();
		c.setTime(nowTime);
		c.add(c.DATE, +date);
		return c.getTime();
	}

	/**
	 * 根据时间得到该时间提前多少小时的时间 例如：如hour是1，即得到的时间是：该时间减去1小时
	 *
	 * @param nowTime
	 * @param hour
	 * @return
	 */
	@SuppressWarnings("static-access")
	public static Date getBeforeHour(Date nowTime, int hour) {
		Calendar c = Calendar.getInstance();
		c.setTime(nowTime);
		c.add(c.HOUR, -hour);
		return c.getTime();
	}

	/**
	 * 根据时间得到该时间延后多少小时的时间 例如：如hour是1，即得到的时间是：该时间加上1小时
	 *
	 * @param nowTime
	 * @param hour
	 * @return
	 */
	@SuppressWarnings("static-access")
	public static Date getAfterHour(Date nowTime, int hour) {
		Calendar c = Calendar.getInstance();
		c.setTime(nowTime);
		c.add(c.HOUR, +hour);
		return c.getTime();
	}

	/**
	 * 时间差 startTime - endTime
	 *
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static Long getTimeDifference(Date startTime, Date endTime) {
		return startTime.getTime() - endTime.getTime();
	}

	/**
	 * 根据秒和毫秒计算出日期(精确到毫秒)
	 *
	 * @param second
	 *            秒
	 * @param milSecond
	 *            毫秒
	 * @return String类型
	 */
	public static String toDateStrBySAndMS(int second, int milSecond) {

		Timestamp ts = new Timestamp((long) second * 1000);

		ts.setNanos(milSecond * 1000);

		Date d = new Date(ts.getTime());

		return yyyy_MM_dd_HH_mm_ssSSS(d);
	}

	/**
	 * 根据秒和毫秒计算出日期(精确到毫秒)
	 *
	 * @param second
	 *            秒
	 * @param milSecond
	 *            毫秒
	 * @return Date类型
	 */
	public static Date toDateBySAndMS(int second, int milSecond) {

		double s = milSecond * 1.0 / 1000;
		double totalS = second + s;
		double totalMS = totalS * 1000;

		Date date = new Date();
		date.setTime(new Double(totalMS).longValue());

		return date;
	}

	/**
	 * 把秒数转成时间(yyyy-MM-dd HH:mi:ss)
	 *
	 * @param second
	 * @return
	 */
	public static String yyyy_MM_dd_HH_mm_ssBySecond(long second) {
		Date date = new Date();
		date.setTime(second * 1000);
		return DateTool.yyyy_MM_dd_HH_mm_ss(date);
	}

	/**
	 * 格式化毫秒,如果不足六位前面补0,例如：t为123,则返回000123
	 *
	 * @param t
	 *            毫秒数
	 * @return
	 */
	public static String formatToSixBymilSecond(String t) {

		String tt = "0";

		if (t.length() == 1) {
			tt = "00000" + t;
		} else if (t.length() == 2) {
			tt = "0000" + t;
		} else if (t.length() == 3) {
			tt = "000" + t;
		} else if (t.length() == 4) {
			tt = "00" + t;
		} else if (t.length() == 5) {
			tt = "0" + t;
		} else if (t.length() == 6) {
			tt = t;
		}

		return tt;
	}

	/**
	 * 根据秒和微秒转成时间,例如：2012-11-22 14:55:50.765333
	 *
	 * @param second
	 *            秒
	 * @param microsecond
	 *            微秒
	 * @return yyyy-MM-dd HH:mi:ss.SSS
	 */
	public static String toMicrosecndStrBySecondAndMicroscond(long second, String microsecond) {

		Date date = new Date();
		date.setTime(second * 1000);
		String dateStr = DateTool.yyyy_MM_dd_HH_mm_ss(date);
		dateStr = dateStr + "." + microsecond;
		return dateStr;
	}

	/**
	 * 根本原表名称，时间，接口ID得到要创建的数据库分表的名称
	 *
	 * @param tableName
	 * @param time
	 * @return
	 */
	public static String getTableName(String tableName, String time) {
		Timestamp stDate = new Timestamp(Long.parseLong(time) * 1000);
		tableName = tableName + "_business_" + yyyyMMdd(stDate);
		return tableName;
	}

	/**
	 * 根本原表名称，时间，接口ID得到要创建的数据库分表的名称
	 *
	 * @param tableName
	 * @param time
	 * @param infoId
	 * @return
	 */
	public static String getTableName(String tableName, String time, String infoId) {
		if (time != null) {
			Timestamp stDate = new Timestamp(Long.parseLong(time) * 1000);
			tableName = tableName + "_" + yyyyMMdd(stDate) + "_" + infoId;
		} else {
			tableName = tableName + "_" + infoId;
		}
		return tableName;
	}

	/**
	 * 根本原表名称，时间，接口ID得到要创建的数据库分表的名称
	 *
	 * @param tableName
	 * @param time
	 * @param infoId
	 * @return
	 */
	public static String getMPLSTableName(String tableName, String time, String infoId) {
		if (time != null) {
			Timestamp stDate = new Timestamp(Long.parseLong(time) * 1000);
			tableName = tableName + "_" + yyyyMMdd(stDate) + "_" + infoId;
		} else {
			tableName = tableName + "_" + yyyyMMdd(new Date()) + "_" + infoId;
		}
		return tableName;
	}

	public static long getDateForLong(String date) {
		SimpleDateFormat fd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date d = fd.parse(date);
			return d.getTime();
		} catch (ParseException e) {
			return 0;
		}
	}



	public static long getMillis(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.getTimeInMillis();
	}

	public static Date parse(String value) {
		SimpleDateFormat df = null;
		if (value.indexOf(':') != -1)
			df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		else
			df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return df.parse(value);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 反转日期
	 * @param value
	 * @param format
	 * @return
	 */
	public static Date parse(String value, String format) {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		try {
			return df.parse(value);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String reduDate(Date date1, int day) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(getMillis(date1) - ((long) day) * 24 * 3600 * 1000);
		return getDate1(c.getTime());
	}

	/**
	 * 加上几分钟
	 *
	 * @param date1
	 * @param min
	 * @return
	 */
	public static String addDate(Date date1, int min) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(getMillis(date1) + ((long) min) * 1000 * 60);
		return getDate1(c.getTime());

	}

	public static String redDate(Date date1, int min) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(getMillis(date1) - ((long) min) * 1000 * 60);
		return getDate1(c.getTime());

	}

	public static String getDate1(Date aDate) {
		SimpleDateFormat df;
		String returnValue = "";

		if (aDate != null) {
			df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			returnValue = df.format(aDate);
		}

		return (returnValue);
	}

	/**
	 * 比较日期大小
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static Long compare(Date date1, Date date2) {
		return getMillis(date1) - getMillis(date2);
	}

	/**
	 * 比较日期大小(字符串)
	 * @param date
	 * @param date1
	 * @return
	 */
	public static Long compare(String date, String date1) {
		Date date2 = parse(date);
		Date date3 = parse(date1);
		return getMillis(date2) - getMillis(date3);
	}

	@SuppressWarnings("deprecation")
	public static Date getDate(int year, int month, int weekInMonth, int dayInWeek) {
		Calendar date = Calendar.getInstance();
		date.clear();
		date.set(Calendar.YEAR, year);
		date.set(Calendar.MONTH, month - 1);
		date.set(Calendar.WEEK_OF_MONTH, weekInMonth);
		date.set(Calendar.DAY_OF_WEEK, dayInWeek);
		Date date2 = date.getTime();
		Date time = date2;
		if ((date2.getMonth() + 1) != month) {
			time = yyyy_MM_dd_HH_mm_ss(addDate(date2, 7 * 24 * 60));
		}
		return time;
	}

	public static Date getDate1(int year, int month, int weekInMonth, int dayInWeek) {
		Calendar date = Calendar.getInstance();
		date.clear();
		date.set(Calendar.YEAR, year);
		date.set(Calendar.MONTH, month - 1);
		date.set(Calendar.WEEK_OF_MONTH, weekInMonth);
		date.set(Calendar.DAY_OF_WEEK, dayInWeek);
		Date date2 = date.getTime();
		return date2;
	}

	public static Date getDate1(int year, int month) {
		Calendar date = Calendar.getInstance();
		date.clear();
		date.set(Calendar.YEAR, year);
		date.set(Calendar.MONTH, month - 1);
		Date date2 = date.getTime();
		return date2;
	}

	/**
	 * 获取上个月的最后一天
	 *
	 * @return
	 */
	public static Date getBeforMonthLastDay() {
		Calendar date = Calendar.getInstance();
		// date.add(Calendar.MONTH-1, 1);
		date.set(Calendar.DAY_OF_MONTH, 0);
		return date.getTime();
	}

	/**
	 * 返回离某天最近的星期几 (往后推)
	 *
	 * @param time
	 * @param weekInMonth
	 * @return
	 */
	@SuppressWarnings("static-access")
	public static String getDate(Date time, int weekInMonth) {
		if (weekInMonth == 0) {
			weekInMonth = 7;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(time);
		int day = calendar.get(calendar.DAY_OF_WEEK) - 1;
		String nowTime = "";
		if (day >= weekInMonth) {
			nowTime = DateTool.addDate(time, (weekInMonth - day) * 24 * 60);

		} else {
			nowTime = DateTool.addDate(time, (weekInMonth - day) * 24 * 60);

		}
		String endTime = nowTime;
		if (DateTool.compare(yyyy_MM_dd_HH_mm_ss(time), nowTime) > 0) {
			endTime = DateTool.addDate(DateTool.yyyy_MM_dd_HH_mm_ss(nowTime), 7 * 24 * 60);
		}
		return endTime;
	}

	/**
	 * 返回离某天最近的星期几
	 *
	 * @param time
	 * @param weekInMonth
	 * @return
	 */
	@SuppressWarnings("static-access")
	public static String getDate1(Date time, int weekInMonth) {
		if (weekInMonth == 0) {
			weekInMonth = 7;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(time);
		int day = calendar.get(calendar.DAY_OF_WEEK) - 1;
		String nowTime = "";
		if (day >= weekInMonth) {
			nowTime = DateTool.addDate(time, (weekInMonth - day) * 24 * 60);

		} else {
			nowTime = DateTool.addDate(time, (weekInMonth - day) * 24 * 60);

		}

		return nowTime;
	}

	public static String getTomorowYYYYMMDD() {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.DAY_OF_YEAR, 1);
		return df.format(c.getTime());
	}

	public static String getTodayYYYYMMDD() {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		return df.format(c.getTime());
	}

	public static String getByforeYYYYMMDD(int day) {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.DAY_OF_YEAR, -day);
		return df.format(c.getTime());
	}

	/**
	 * 根据传入的时间获取该时间的小时
	 *
	 * @param date
	 * @return
	 */
	public static int getHour(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		return hour;
	}

	/**
	 * 获取去掉分钟和秒的时间
	 *
	 * @param date
	 * @return
	 */
	public static Date getDate_yyyy_MM_dd_HH(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
		String s = sdf.format(date);
		Date tDate = null;
		try {
			tDate = sdf.parse(s);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return tDate;
	}

	/**
	 * 获取去掉小时、分钟和秒的时间
	 *
	 * @param date
	 * @return
	 */
	public static Date getDate_yyyy_MM_dd(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String s = sdf.format(date);
		Date tDate = null;
		try {
			tDate = sdf.parse(s);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return tDate;
	}

	/**
	 * 判断某个时间是否是整点
	 *
	 * @param date
	 * @return
	 */
	public static boolean isIntegralHour(Date date) {
		if (date == null) {
			return false;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("mmss");
		String mmss = sdf.format(date);
		return "0000".equals(mmss);
	}

	/**
	 * 判断某个时间是否是整天
	 *
	 * @param date
	 * @return
	 */
	public static boolean isIntegralDay(Date date) {
		if (date == null) {
			return false;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
		String mmss = sdf.format(date);
		return "000000".equals(mmss);
	}

	/**
	 * 把startDate到endDate这段时间内按照minuteSlice分钟大小分成n片，
	 * 如果某片跨越不同的天，则把结束时间当天的开始时间前的时间去除，即只把结束时间当天的时间分片 并且把最后一个小于分片大小的分片合并到前一个分片。
	 * 如：开始时间：2019-12-04 23:49:51，结束时间：2019-12-05 00:18:51，分片的大小为5分钟。 结果：
	 * (2019-12-05 00:00:00, 2019-12-05 00:05:00) 注意：丢弃当天前的所有时间 (2019-12-05
	 * 00:05:00, 2019-12-05 00:10:00) (2019-12-05 00:10:00, 2019-12-05 00:18:51)
	 * 注意：合并最后1个分片
	 *
	 * @param startDate
	 *            开始时间
	 * @param endDate
	 *            结束时间
	 * @param minuteSlice
	 *            分片的大小（单位：分钟），大小：大于0
	 * @return 返回list集合，集合中为Date数组，数组内保存分片的开始时间和结束时间。
	 */
	public static List<Date[]> getSliceDateOfDayList(Date startDate, Date endDate, long minuteSlice) {
		List<Date[]> list = new ArrayList<Date[]>();
		if (startDate == null || endDate == null || minuteSlice <= 0
				|| (startDate.getTime() - endDate.getTime()) >= 0) {
			return null;
		}
		// 结束时间不是整天，并且开始时间与结束时间跨天，则把开始时间设置为结束时间的当天的最开始时间
		if (!isIntegralDay(endDate)
				&& (getDate_yyyy_MM_dd(startDate).getTime() - getDate_yyyy_MM_dd(endDate).getTime() != 0)) {
			startDate = getDate_yyyy_MM_dd(endDate);
		}
		boolean flag = true;
		while (flag) {
			Date minDate = new Date(startDate.getTime() + minuteSlice * (60 * 1000));
			if (minDate.getTime() > endDate.getTime()) { // 某片的开始时间加上分片时间超过最后的结束时间的时候
				Date data[] = new Date[2];
				int size = list.size();
				if (size != 0) {
					Date[] tempDate = list.get(size - 1);
					data[0] = tempDate[0];
					list.remove(size - 1);
				} else {
					data[0] = startDate;
				}
				data[1] = endDate;
				list.add(data);
				flag = false;
			} else if (minDate.getTime() == endDate.getTime()) { // 某片的开始时间加上分片时间刚好等于最后的结束时间的时候
				Date data[] = new Date[2];
				data[0] = startDate;
				data[1] = endDate;
				list.add(data);
				flag = false;
			} else { // 某片的开始时间加上分片时间没有超过最后的结束时间的时候
				Date data[] = new Date[2];
				data[0] = startDate;
				data[1] = minDate;
				list.add(data);
				startDate = minDate;
			}
		}
		return list;
	}

	/**
	 * 将日期转成时间戳
	 *
	 * @param s
	 * @return
	 * @throws ParseException
	 */
	public static String dateToStamp(String s) throws ParseException {
		String logTimeMilli;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = simpleDateFormat.parse(s);
		long logtime = date.getTime();
		logTimeMilli = String.valueOf(logtime);
		return logTimeMilli;
	}

	/**
	 * 将日期转成时间戳
	 *
	 * @param s
	 * @return
	 * @throws ParseException
	 */
	public static Long dateToLongStamp(String s) throws ParseException {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = simpleDateFormat.parse(s);
		Long logtime = date.getTime();
		return logtime;
	}


	// ======================前端================================
	/**
	 * 返回有日期格式的连续表名集合
	 *
	 * @param tablename
	 *            表名
	 * @param startTime
	 *            开始时间
	 * @param endTime
	 *            结束时间
	 * @return List<String>
	 */
	public static List<String> getTablesNameByTime(String tablename, String startTime, String endTime) {
		List<String> tablesNameList = new ArrayList<String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String startStr = startTime.substring(0, startTime.indexOf(" "));
		String endStr = endTime.substring(0, endTime.indexOf(" "));

		long time = parse(endStr).getTime() - parse(startStr).getTime();
		for (int i = 0; i <= time / 86400000; i++) {
			Date date = new Date(parse(startStr).getTime() + 86400000L * i);
			tablesNameList.add(tablename + "_" + sdf.format(date));
		}
		return tablesNameList;
	}

	/**
	 * 返回有日期格式的表名(用于判断跨天的情况)
	 *
	 * @param tablename
	 *            表名
	 * @param startTime
	 *            开始时间
	 * @param endTime
	 *            结束时间
	 * @return List<String>
	 */
	public static List<String> getTablesName(String tablename, String startTime, String endTime) {
		List<String> tablesNameList = new ArrayList<String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String start = startTime.substring(0, startTime.indexOf(" "));
		String end = endTime.substring(0, endTime.indexOf(" "));
		long time = parse(end).getTime() - parse(start).getTime();
		if ("00:00:00".equals(endTime.substring(endTime.indexOf(" ") + 1)))
			time -= 1;
		for (int i = 0; i <= time / 86400000; i++) {
			Date date = new Date(parse(start).getTime() + 86400000L * i);
			tablesNameList.add(tablename + "_" + sdf.format(date));
		}
		return tablesNameList;
	}

	public static List<String> getTablesNameNoSplit(String tablename, String startTime, String endTime) {
		List<String> tablesNameList = new ArrayList<String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String start = startTime.substring(0, startTime.indexOf(" "));
		String end = endTime.substring(0, endTime.indexOf(" "));
		long time = parse(end).getTime() - parse(start).getTime();
		if ("00:00:00".equals(endTime.substring(endTime.indexOf(" ") + 1)))
			time -= 1;
		for (int i = 0; i <= time / 86400000; i++) {
			Date date = new Date(parse(start).getTime() + 86400000L * i);
			tablesNameList.add(tablename + "" + sdf.format(date));
		}
		return tablesNameList;
	}

	/**
	 * 根据startTime和endTime 获取表名(允许跨天)
	 * @param tableName
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static List<String> getTablesNameByCrossDay(String tableName, String startTime, String endTime){
		// 参数校验
		if (StringUtils.isBlank(tableName) || StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime)){
			throw new NullPointerException("tableName and startTime and endTime must not null");
		}
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		DateTimeFormatter dayFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
		LocalDateTime startDateTime = LocalDateTime.parse(startTime, dateTimeFormatter);
		LocalDateTime endDateTime = LocalDateTime.parse(endTime, dateTimeFormatter);

		List<String> list = new ArrayList<>();
		LocalDateTime startDateBeforeTime = startDateTime.minusDays(1L);
		while (startDateBeforeTime.isBefore(endDateTime)){
			if (!tableName.endsWith("_")){
				tableName = tableName + "_";
			}
			list.add(tableName + startDateBeforeTime.format(dayFormatter));
			startDateBeforeTime = startDateBeforeTime.plusDays(1L);
		}
		return list;
	}

	public static List<String> getTablesNameAndYesterday(String tablename, String startTime, String endTime) {
		List<String> tablesNameList = new ArrayList<String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String start = startTime.substring(0, startTime.indexOf(" "));
		String end = endTime.substring(0, endTime.indexOf(" "));
		long time = parse(end).getTime() - parse(start).getTime();
		if ("00:00:00".equals(endTime.substring(endTime.indexOf(" ") + 1)))
			time -= 1;
		Date date = null;
		for (int i = 0; i <= time / 86400000; i++) {
			date = new Date(parse(start).getTime() + 86400000L * i);
			tablesNameList.add(tablename + "_" + sdf.format(date));
		}
		if (tablesNameList.size() == 1 && date != null) {
			tablesNameList.add(tablename + "_" + sdf.format(getBeforeDate(date, 1)));
		}
		return tablesNameList;
	}

	/**
	 * 获取表的时间后缀
	 *
	 * @param startTime
	 *            开始时间
	 * @return String
	 */
	public static String getDateSuffix(String startTime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String start = startTime.substring(0, startTime.indexOf(" "));
		Date date = new Date(parse(start).getTime());
		return sdf.format(date);
	}

	// 判断两个时间是否跨天,参数单位：微秒
	public static List<String> compPass(Long time1, Long time2) {
		// System.out.println("----判断表后缀---time1：" + time1);
		// System.out.println("----判断表后缀---time2：" + time2);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		List<String> timeList = new ArrayList<String>();
		// 1. 先将time1，转成yyyy-MM-dd hh
		Long t1 = time1 / 1000 / 1000 / 60; // 转化成天
		Date date1 = new Date(t1 * 60 * 1000);
		timeList.add(sdf.format(date1));

		// 2. 将1的结果加上1天
		Long t2 = (t1 * 60 * 1000 + 24 * 60 * 60 * 1000L) * 1000;
		// 3. 判断time2是否大于2的结果，大于则跨小时
		if (time2 >= t2) {
			// System.out.println("------------跨天,t2:" + t2);
			Date date2 = new Date(t2 / 1000);
			timeList.add(sdf.format(date2));
		}
		return timeList;
	}

	// 判断两个时间是否跨小时,参数单位：微秒
	public static List<String> compPassHours(Long time1, Long time2) {
		// System.out.println("----判断表后缀---time1：" + time1);
		// System.out.println("----判断表后缀---time2：" + time2);
		List<String> timeList = new ArrayList<String>();
		// 1. 先将time1，转成yyyy-MM-dd hh
		Long t1 = time1 / 1000 / 1000 / 60 / 60; // 转化成小时
		Date date1 = new Date(t1 * 60 * 60 * 1000);
		timeList.add(changeDateTime(date1));

		// 2. 将1的结果加上1小时
		Long t2 = (t1 * 60 * 60 * 1000 + 60 * 60 * 1000L) * 1000;
		// 3. 判断time2是否大于2的结果，大于则跨小时
		if (time2 >= t2) {
			// System.out.println("------------跨小时,t2:" + t2);
			Date date2 = new Date(t2 / 1000);
			timeList.add(changeDateTime(date2));
		}
		return timeList;
	}

	private static String changeDateTime(Date date) {
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat sdf3 = new SimpleDateFormat("HH");
		String dateStr1 = sdf2.format(date);
		String timeStr1 = sdf3.format(date);
		if (timeStr1.startsWith("0")) {
			Integer hour = Integer.parseInt(timeStr1.substring(1));
			hour += 1;
			if (hour > 9) {
				return dateStr1 + hour;
			} else {
				return dateStr1 + "0" + hour;
			}
		} else {
			Integer hour = Integer.parseInt(timeStr1);
			return dateStr1 + (hour + 1);
		}
	}

	/**
	 * 根据传进来的时间获取表名(business_20180723)
	 *
	 * @param tableName
	 * @param endTimeUsec
	 *            微秒
	 * @return String
	 */
	public static String getTableNameIsDate3(String tableName, Long endTimeUsec) {
		String yyyy_MM_dd_HH_mm_ss = DateTool.yyyy_MM_dd_HH_mm_ssBySecond(endTimeUsec / 1000000);
		String[] split = yyyy_MM_dd_HH_mm_ss.split(" ");
		if (split.length == 2) {
			String yyyyMMdd = split[0].replace("-", "");
			return tableName + "_" + yyyyMMdd;
		}
		return tableName;
	}

	/**
	 * 根据startTimeUsec和endTimeUsec获取小时表表名(business_2018072301)
	 *
	 * @param tableName
	 * @param startTimeUsec
	 * @param endTimeUsec
	 * @return List<String>
	 */
	public static List<String> getHoursTable(String tableName, Long startTimeUsec, Long endTimeUsec) {
		List<String> timeList = new ArrayList<String>();
		Long time = endTimeUsec / 1000000 - startTimeUsec / 1000000;
		Long count = time % 3600 == 0 ? time / 3600 : time / 3600 + 1;
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat sdf3 = new SimpleDateFormat("HH");
		String dateStr1 = sdf2.format(startTimeUsec / 1000);
		String timeStr1 = sdf3.format(startTimeUsec / 1000);
		for (int i = 0; i <= count; i++) {
			if (timeStr1.startsWith("0")) {
				Integer hour = Integer.parseInt(timeStr1.substring(1));
				hour += i;
				if (hour > 9) {
					timeList.add(tableName + "_" + dateStr1 + hour);
				} else {
					timeList.add(tableName + "_" + dateStr1 + "0" + hour);
				}
			} else {
				Integer hour = Integer.parseInt(timeStr1);
				hour += i;
				timeList.add(tableName + "_" + dateStr1 + hour);
			}
		}
		if (endTimeUsec-startTimeUsec== 3600L *24*1000*1000){
			timeList.remove(timeList.size()-1);
		}
		return timeList;
	}


	/**
	 * 根据startTimeUsec和endTimeUsec获取小时表表名(business_2018072300)
	 *
	 * @param tableName
	 * @param startTimeUsec
	 * @param endTimeUsec
	 * @return List<String>
	 */
	public static List<String> getHoursTable2(String tableName, Long startTimeUsec, Long endTimeUsec) {
		List<String> timeList = new ArrayList<String>();
		Long time = endTimeUsec / 1000000 - startTimeUsec / 1000000;
		Long count = time % 3600 == 0 ? time / 3600 : time / 3600 + 1;
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat sdf3 = new SimpleDateFormat("HH");
		String dateStr1 = sdf2.format(startTimeUsec / 1000);
		String timeStr1 = sdf3.format(startTimeUsec / 1000);
		for (int i = 0; i < count; i++) {
			if (timeStr1.startsWith("0")) {
				Integer hour = Integer.parseInt(timeStr1.substring(1));
				hour += i;
				if (hour > 9) {
					timeList.add(tableName + "_" + dateStr1 + hour);
				} else {
					timeList.add(tableName + "_" + dateStr1 + "0" + hour);
				}
			} else {
				Integer hour = Integer.parseInt(timeStr1);
				hour += i;
				timeList.add(tableName + "_" + dateStr1 + hour);
			}
		}
		return timeList;
	}

	/**
	 *
	 * @Description: 列出两个日期之间的所有日期时间
	 * @Title: listAllDate
	 * @param @param startDay yyyy-MM-dd HH:mm:ss
	 * @param @param endDay yyyy-MM-dd HH:mm:ss
	 * @param @return 设定文件
	 * @return List 返回类型
	 * @throws
	 */
	public static List<String> listAllDateTime(String start, String end) {
		SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
		Calendar startDay = Calendar.getInstance();
		Calendar endDay = Calendar.getInstance();
		List<String> list = new ArrayList<String>();
		Date startDate = null;
		Date endDate = null;
		try {
			startDate = df1.parse(start);
			endDate = df1.parse(end);
			list.add(df1.format(startDate));
			startDay.setTime(startDate);
			endDay.setTime(endDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 给出的日期开始日比终了日大则不执行打印
		if (startDay.compareTo(endDay) >= 0) {
			return list;
		}
		// 现在打印中的日期
		Calendar currentPrintDay = startDay;
		while (true) {
			// 日期加一
			currentPrintDay.add(Calendar.MINUTE, 1);
			// 日期加一后判断是否达到终了日，达到则终止打印
			if (currentPrintDay.compareTo(endDay) >= 0) {
				break;
			}
			list.add(df1.format(currentPrintDay.getTime()));
			// 打印日期
			//logger.info(df1.format(currentPrintDay.getTime()));
		}
		//list.add(end);
		list.add(df1.format(endDate));
		return list;
	}

	/**
	 *
	 * @Description: 列出两个日期之间的所有日期时间
	 * @Title: listAllDate
	 * @param @param startDay yyyy-MM-dd HH:mm:ss
	 * @param @param endDay yyyy-MM-dd HH:mm:ss
	 * @param @return 设定文件
	 * @return List 返回类型
	 * @throws
	 */
	public static List<Long> listAllTime(String start, String end) {
		SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar startDay = Calendar.getInstance();
		Calendar endDay = Calendar.getInstance();
		List<Long> list = new ArrayList<Long>();
		Date startDate = null;
		Date endDate = null;
		try {
			startDate = df1.parse(start);
			endDate = df1.parse(end);
			list.add(startDate.getTime());
			startDay.setTime(startDate);
			endDay.setTime(endDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 给出的日期开始日比终了日大则不执行打印
		if (startDay.compareTo(endDay) >= 0) {
			return list;
		}
		// 现在打印中的日期
		Calendar currentPrintDay = startDay;
		while (true) {
			// 日期加一
			currentPrintDay.add(Calendar.MINUTE, 1);
			// 日期加一后判断是否达到终了日，达到则终止打印
			if (currentPrintDay.compareTo(endDay) >= 0) {
				break;
			}
			list.add(currentPrintDay.getTime().getTime());
			// 打印日期
			//logger.info(df1.format(currentPrintDay.getTime()));
		}
		//list.add(end);
		//logger.info(DateTool.yyyy_MM_dd_HH_mm_ss(endDate));
		list.add(endDate.getTime());
		return list;
	}

	/**
	 * 传进来的时间(ms毫秒)
	 *
	 * 保留两位小数，如:2 会在后面补上00 即2.00
	 * 1）   如果时长/耗时大于等于60000ms 则格式化为 “XX分XX.XX秒”保留两位小数
	 * 2）   如果时长/耗时小于60000ms大于等于1000ms则格式化为“XX.XX秒”保留两位小数
	 * 3）   如果时长/耗时小于1000ms则格式化为“XX.XX毫秒”保留两位小数
	 * 如:
	 * 204266ms  显示为  3分24.27秒
	 * 2008ms    显示为  2.01秒（4舍5入）
	 * 4ms       显示为  4.00毫秒（4舍5入）
	 *
	 * @param data 传进来的时间(ms毫秒)
	 * @returns
	 */
	public static String getMinSecond(double data) {
		StringBuffer sb = new StringBuffer();

		if(data >= 60000){
			long minute = (long)Math.floor(data / 60000);
			sb.append(minute);
			sb.append("分");

			double second = data % 60000 / 1000;
			if(second != 0) {
				String secondStr = String.format("%.2f",second);
				sb.append(secondStr);
				sb.append("秒");
			}
		}else if(data >= 1000 && data < 60000){
			long second =(long) Math.floor(data / 1000);
			double millisecond = data % 1000 / 1000;

			sb.append(String.format("%.2f",(second + millisecond )) + "秒" );
		}else{
			sb.append(String.format("%.2f",data) + "毫秒");
		}
		return sb.toString();
	}

	public static String stampToDate(String datestr) {
		SimpleDateFormat fmat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Long d = new Long(datestr);
		Date date = new Date(d*1000);
		return fmat.format(date);
	}
	// ============================================================


}
